package org.dy.db.model;

import java.util.ArrayList;
import java.util.List;

import org.dy.config.XmlConvertable;
import org.jdom.Attribute;
import org.jdom.Element;

public class TableHeader implements XmlConvertable {
	
	String tableName;
	boolean isView = false;
	String pkName;
	
	// chmod(1,2,4) 
	// in general there are 1 3 5 7
	// read add/update delete
	int mod = 7;
	
	boolean isCache = false;
	int cacherefreshPeriod;
	
	String des;
	
	List<FieldInfo> fields = new ArrayList<FieldInfo>();

	public List<FieldInfo> getFields() {
		return fields;
	}

	public String getTableName() {
		return tableName;
	}

	public void setTableName(String tableName) {
		this.tableName = tableName;
	}
	
	public int getColumnSize(){
		return this.fields.size();
	}
	
	public FieldInfo getPkInfo(){
		if(this.fields != null){
			for(FieldInfo fi:fields){
				if(fi.isPk){
					FieldInfo fbean = new FieldInfo(fi);	
					return fbean ;
				}
			}	
		}
		return null;
	}
	
	public List<FieldInfo> getFkInfoList(){
		List<FieldInfo> rl = new ArrayList<FieldInfo>();
		if(this.fields != null){
			for(FieldInfo fi:fields){
				if(fi.isFk){
					FieldInfo fbean = new FieldInfo(fi);	
					rl.add(fbean);
				}
			}	
		}
		return rl ;
	}
	
	public FieldInfo getFieldByName(String name){
		if(this.fields != null){
			for(FieldInfo fi:fields){
				if(name.equalsIgnoreCase(fi.name)){
					FieldInfo fbean = new FieldInfo(fi);	
					return fbean ;
				}
			}	
		}
		return null;
	}
	
	// xml interchange
	public Element getXmlElement(){
		Element ee=new Element("table_header");
		ee.setAttribute("table_name", this.tableName + "");
		ee.setAttribute("des", this.des + "");
		ee.setAttribute("mod", this.mod + "");
		ee.setAttribute("is_view", this.isView + "");
		if(this.isCache){
			ee.setAttribute("is_cache", this.isCache + "");
			ee.setAttribute("refresh_period", this.cacherefreshPeriod + "");
		}
		for(FieldInfo field:fields){
			ee.addContent(field.getXmlElement());
		}
		return ee;
	}
	
	@SuppressWarnings("unchecked")
	public void getFromXml(Element el) {
		// TODO Auto-generated method stub
		this.tableName = el.getAttributeValue("table_name");
		
		Attribute desAttr = el.getAttribute("des");
		if(desAttr != null)
			this.des = desAttr.getValue();
		
		Attribute modAttr = el.getAttribute("mod");
		if(modAttr != null)
			this.mod = Integer.parseInt(modAttr.getValue());
		
		Attribute cacheAttr = el.getAttribute("is_cache");
		if(cacheAttr != null){
			this.isCache = Boolean.valueOf(cacheAttr.getValue());
			Attribute rpAttr = el.getAttribute("refresh_period");
			if(rpAttr != null){
				this.cacherefreshPeriod = Integer.parseInt(rpAttr.getValue());
			}
		}
		
		Attribute isViewAttr = el.getAttribute("is_view");
		if(isViewAttr != null)
			this.isView = Boolean.valueOf(isViewAttr.getValue());
		
		List<Element> fe = el.getChildren("field");
		
		for(Element ee:fe){
			FieldInfo fbean = new FieldInfo();
			fbean.getFromXml(ee);
			fbean.seq = fe.indexOf(ee);
			if(fbean.isPk)
				this.pkName = fbean.name;
			fields.add(fbean);
		}
	}

	public boolean isView() {
		return isView;
	}

	public void setView(boolean isView) {
		this.isView = isView;
	}

	public String getPkName() {
		return pkName;
	}

	public void setPkName(String pkName) {
		this.pkName = pkName;
	}

	public int getMod() {
		return mod;
	}

	public void setMod(int mod) {
		this.mod = mod;
	}
	
	public String toString() {
		StringBuffer sb = new StringBuffer();
		sb.append("{fields = ");
		sb.append("[");
		for(FieldInfo fi:fields){
			sb.append(fi.toString());
			if(fields.indexOf(fi) != fields.size() - 1)
				sb.append(",");
		}
		sb.append("]");
		sb.append("}");
		return sb.toString();
	}

	public int getCacherefreshPeriod() {
		return cacherefreshPeriod;
	}

	public void setCacherefreshPeriod(int cacherefreshPeriod) {
		this.cacherefreshPeriod = cacherefreshPeriod;
	}

	public boolean isCache() {
		return isCache;
	}

	public void setCache(boolean isCache) {
		this.isCache = isCache;
	}

	public String getDes() {
		return des;
	}

	public void setDes(String des) {
		this.des = des;
	}
}
